package com.xiaoyu.client.adapter.mall;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.xiaoyu.client.R;
import com.xiaoyu.client.model.goods.MallHomeBean.DataBean.MallTagBean;

import java.util.ArrayList;
import java.util.List;

public class MallTypeAdapter extends BaseAdapter {
    private Context mContext;
    private List<MallTagBean> mList = new ArrayList<>();

    public MallTypeAdapter(Context mContext, List<MallTagBean> mList) {
        this.mContext = mContext;
        this.mList = mList;
    }

    @Override
    public int getCount() {
        if (mList.size() >= 7)
            return 8;
        else
            return mList.size()+1;
    }

    @Override
    public MallTagBean getItem(int position) {
        if (mList.size() == 0)
            return null;
        else
            return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder{
        ImageView tagImg;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_shop_tag,null);
            holder = new ViewHolder();
            holder.tagImg = convertView.findViewById(R.id.item_shop_tag_img_name);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (position == getCount()-1){
            holder.tagImg.setImageResource(R.mipmap.icon_more_tag);
        }else{
            Glide.with(mContext).load(getItem(position).getGoodstagimg()).into(holder.tagImg);
        }

        return convertView;
    }

    public void updateList(List<MallTagBean> mList){
        this.mList = mList;
        this.notifyDataSetChanged();
    }
}
